package com.qianchang.microfinance.dao;

import android.content.Context;
import android.os.Environment;

import com.qianchang.microfinance.bean.PolicySet;
import com.dm.library.utils.db.DbUtils;
import com.dm.library.utils.db.exception.DbException;
import com.dm.library.utils.db.sqlite.Selector;

import java.util.List;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述: 策略集
 * 修 改 人: sushuo
 * 修改时间: 2016-10-8
 */
public class PolicySetDao {

    private static PolicySetDao policySetDao;

    private PolicySetDao() {
//        dbUtils = DbUtils.create(context, getInnerSDCardPath(), "PolicySet.db");
    }

    public static PolicySetDao getInstance() {
        if (policySetDao == null) {
            policySetDao = new PolicySetDao();
        }
        return policySetDao;
    }

    public String getInnerSDCardPath() {
        return Environment.getExternalStorageDirectory().getPath();
    }

    /**
     * 根据参数名获取配置策略信息
     *
     * @param context
     * @param paramName
     * @return
     */
    public PolicySet findPolicySetByParamName(Context context, String paramName) {
        try {
            return DbUtils.create(context).findFirst(Selector.from(PolicySet.class).where("paramName", "=", paramName));
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    public long count(Context context, String paramName){
        try {
            return DbUtils.create(context).count(Selector.from(PolicySet.class).where("paramName", "=", paramName));
        } catch (DbException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public long count(Context context){
        try {
            return DbUtils.create(context).count(PolicySet.class);
        } catch (DbException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 保存配置策略
     *
     * @param context
     * @param policySetList
     */
    public void saveAll(Context context, List<PolicySet> policySetList) {
        try {
            DbUtils.create(context).saveAll(policySetList);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新
     *
     * @param context
     * @param policySetList
     */
    public void updateAll(Context context, List<PolicySet> policySetList){
        try {
            DbUtils.create(context).updateAll(policySetList, "paramValue");
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据主键判断是否存在，存在就删除重新添加，不存在就直接添加
     *
     * @param context
     * @param policySetList
     */
    public void replaceAll(Context context, List<PolicySet> policySetList){
        try {
            DbUtils.create(context).replaceAll(policySetList);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public List<PolicySet> findAll(Context context){
        try {
            return DbUtils.create(context).findAll(PolicySet.class);
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除所有记录
     *
     * @param context
     */
    public void deleteAll(Context context) {
        try {
            DbUtils.create(context).deleteAll(PolicySet.class);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

}
